import { Component, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { CommunityEditComponent } from '../community-edit/community-edit.component';
import { MenuNodeItem } from '../../../menu-manage/menu.service';
import { AreaService } from '../../area.service';
import { ApiService } from '@core/services/api';
import {HttpClient} from "@angular/common/http";
import { PermissionService } from '@core/services/permission.service';
import { ACLService } from '@delon/acl';
import { STData } from '@delon/abc';
import { CardFaceSetComponent } from '../card-face-set/card-face-set.component';
import { param } from 'jquery';

@Component({
  selector: 'app-community-list',
  templateUrl: './community-list.component.html',
  styles: []
})
export class CommunityListComponent implements OnInit {
  @ViewChild('st', {static: false})st: TablePageComponent;
  addFlag:boolean = this.aclService.canAbility("area-manage/add");//检查用户按钮权限
  editFlag:boolean =  this.aclService.canAbility("area-manage/edit");
  delFlag:boolean =  this.aclService.canAbility("area-manage/delete");
  key:string="";
  ip:string="";
  config: SPage = {
    columns: [
      {
        index: 'id',
        title: '选中',
        type: ColumnType.checkbox
      },
      {
        index: 'areaName',
        title: '小区名称',
        width:200,
        filter: {
          type: 'string'
        }
      },
      {
        index: 'areaParentAllName',
        title: '所属区域',
        //className:'w200',
        fFn: (item, cloumn): string => {
          const arr = item[cloumn.indexKey].split(',');
          arr.pop();
          return arr.join('')
        },
        filter: {
          type: 'string'
        }
      },
      {
        index: 'areaRank',
        title: '类型',
        width:120,
        type: ColumnType.tag,
        tag: {
          6: {text:'小区(村)'},
          5: {text:'社区'},
        },
        // filter: {
        //   type: 'select',
        //   items: [
        //     {label: '社区', value:'5'},
        //     {label: '小区（村）', value:'6'},
        //     // {label: '村组（巷）', value: '7'},
        //   ]
        // }
      },
      {
        index: 'status',
        title: '数据状态',
        width:120,
        type: ColumnType.tag,
        tag: {
          false: {text:'停用', color: 'red'},
          true: {text:'启用', color: 'green'},
        },
        filter: {
          type: 'select',
          hideSearch: true,
          items: [
            {label: '启用', value: true},
            {label: '停用', value: false},
          ]
        }
      },
      {
        index: 'cardBindFace',
        title: '卡和人脸绑定',
        width:120,
        type: ColumnType.tag,
        
        tag: {
          1: {text:'绑定', color: 'green'},
          0: {text:'未绑定', color: 'red'},
        },
      },
      {
        index: 'sipIsIssue',
        title: '呼叫开关',
        width:120,
        type: ColumnType.tag,
        tag: {
          1: {text:'开', color: 'green'},
          0: {text:'关', color: 'red'},
        },
      },
      {
        index: 'createTime',
        title: '创建时间',
        width:200
      },
      /*{
        index: 'areaRemark',
        title: '备注'
      },*/
    ],
    reqUrl: this.api.area.page.url,
    editBtn: [
      {title: '编辑', iif: () => this.editFlag},
      {title: '设置',iif:(item:STData)=>{
          return item.areaRank == 6;
      } }
    ]
  };
  selectedData: object[];
  //数据预处理
  dataProcess(data: STData[]){
    console.log(data);
    for(let item of data){
      item.cardBindFace= item.areaRank != 6 ? null : item.cardBindFace
    }
    return data;
  }

  editDic(type: 0 | 1 = 0, param?: object): void {
    this.modal.create({
      nzTitle: type === 0 ? '新增区域' : '编辑区域',
      nzContent: CommunityEditComponent,
      nzWidth: 800,
      nzComponentParams: {
        type,
        param
      },
      nzOnOk: () => {
        this.st.reload();
      },
      nzFooter: null
    })
  }
  _selectChange(e): void {
    this.selectedData = e;
  }
  operate(e): void {
    console.log(e);
    if (e.title === '编辑') {
      this.editDic(1 , e.data)
    }else if(e.title === '设置'){
      let _param = e.data;
      this.modal.create({
        nzTitle:"设置",
        nzContent:CardFaceSetComponent,
        nzWidth:500,
        nzComponentParams:{
          _param
        },
        nzOnOk:() => {
          this.st.reload();
        },
        nzFooter:null
      })
    }
  }
  deleteDic(): void {
    if (!this.selectedData || this.selectedData.length === 0) {
      this.msg.warning('请选择数据后操作');
      return;
    }
    this.modal.confirm({
      nzTitle: '您确定要删除这些数据吗',
      nzOnOk: () => {
        const body = [];
        this.selectedData.forEach((item: any) => {
          body.push(item.id);
        })
        this.http.request(this.api.area.delete.method,this.api.area.delete.url,{body}).subscribe((res:any) => {
          if (res.status === '200') {
            this.msg.success('删除成功');
            this.st.reload();
          }else{
            this.msg.error(res.message)
          }
        })
      }
    })
  }
  selectChange(event): void {
    this.st.searchData({areaParentId: event.keys[0]});
  }

  //呼叫
  sendCall():void{
    window.open("../../../../../../assets/index.html?serno="+this.key+"&type=0") //0代表公网呼叫
  }
  sendCallIP(){
    // window.open("../../../../../../assets/index.html?serno="+this.key+"&type=1&ip="+this.ip)// 1代表局域网呼叫
    window.open("http://"+this.ip+":8080")
  }
  constructor(
    private modal: NzModalService,
    private msg: NzMessageService,
    private area: AreaService,
    private api: ApiService,
    private http: HttpClient,
    private permission: PermissionService,
    private aclService: ACLService
  ) { }


  ngOnInit() {
  
  }
}
